//
//  THNickNameLabel.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THNickNameLabel.h"

#define HQScanWidth 40

@interface THNickNameLabel()

@property (nonatomic, strong) UILabel *bgLabel;

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation THNickNameLabel

/**子类添加控件 */
-(void)setupUI
{
    _duration = 2.0f;
    _animation = NO;
    [self addSubview:self.bgLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.imageV];
}

/**子类控件属性值配置 */
-(void)configViews
{
    [self drawPath];
}

/**子类控件frame配置 */
-(void)setupFrames
{
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    
    [self.bgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    
}


//待优化
- (void)layoutSubviews
{
    [super layoutSubviews];
    if (_animation) {
        self.imageV.hidden = NO;
        self.bgLabel.hidden = NO;
        [self drawPath];

        self.imageV.frame = CGRectMake(0, 0, HQScanWidth, self.contentLabel.size.height);
        self.imageV.layer.mask = self.bgLabel.layer;
        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
        basicAnimation.duration = _duration;
        basicAnimation.fromValue = @(self.imageV.layer.position.x-HQScanWidth);
        basicAnimation.toValue = @(self.imageV.layer.position.x + self.bgLabel.frame.size.width);
        basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        basicAnimation.repeatCount = MAXFLOAT;
        basicAnimation.removedOnCompletion = NO;
        basicAnimation.fillMode = kCAFillModeForwards;
        [self.imageV.layer addAnimation:basicAnimation forKey:@"THGradientLabelLayerX"];

        CABasicAnimation *basicAnimation2 = [CABasicAnimation animationWithKeyPath:@"position.x"];
        basicAnimation2.duration = _duration;
        basicAnimation2.fromValue = @(self.bgLabel.layer.position.x+HQScanWidth);
        basicAnimation2.toValue = @(self.bgLabel.layer.position.x - self.bgLabel.frame.size.width);
        basicAnimation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        basicAnimation2.repeatCount = MAXFLOAT;
        basicAnimation2.removedOnCompletion = NO;
        basicAnimation2.fillMode = kCAFillModeForwards;
        [self.bgLabel.layer addAnimation:basicAnimation2 forKey:@"THGradientLabelBgX"];

    }else{
        self.bgLabel.hidden = YES;
        self.imageV.hidden = YES;
        if ([self.imageV.layer animationForKey:@"THGradientLabelLayerX"]) {
            [self.imageV.layer removeAnimationForKey:@"THGradientLabelLayerX"];
        }
        if ([self.bgLabel.layer animationForKey:@"THGradientLabelBgX"]) {
            [self.bgLabel.layer removeAnimationForKey:@"THGradientLabelBgX"];
        }
    }
}

- (void)drawLinearGradient:(CGContextRef)context
                      path:(CGPathRef)path
                startColor:(CGColorRef)startColor
                  endColor:(CGColorRef)endColor
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };
    
    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    
    
    CGRect pathRect = CGPathGetBoundingBox(path);
    
    //具体方向可根据需求修改
//    CGPoint startPoint = CGPointMake(0, CGRectGetMaxY(pathRect)+7);
//    CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), -7);
    
    CGPoint startPoint = CGPointMake(0, 0);
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), 0);
    
    CGContextSaveGState(context);
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

- (void)drawPath {
    if (!_startColor || !_endColor) {
        return;
    }
    
    //创建CGContextRef
    UIGraphicsBeginImageContext(CGSizeMake(40, 28));
    CGContextRef gc = UIGraphicsGetCurrentContext();
    
    //创建CGMutablePathRef
    CGMutablePathRef path = CGPathCreateMutable();
    
    //绘制Path
    CGSize size = CGSizeMake(40, 28);
    CGFloat difSpace = 5;//倾斜的宽度
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, size.width - difSpace, 0);
    CGPathAddLineToPoint(path, NULL, size.width, size.height);
    CGPathAddLineToPoint(path, NULL, difSpace, size.height);
    CGPathAddLineToPoint(path, NULL, 0, size.height);
    CGPathAddLineToPoint(path, NULL, 0, 0);
    CGPathCloseSubpath(path);
    
    //绘制渐变
    [self drawLinearGradient:gc path:path startColor:(_startColor?:[UIColor clearColor]).CGColor endColor:(_endColor?:[UIColor clearColor]).CGColor];
    
    //注意释放CGMutablePathRef
    CGPathRelease(path);
    
    //从Context中获取图像，并显示在界面上
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.imageV.image = img;
}

- (void)setStartColor:(UIColor *)startColor endColor:(UIColor *)endColor;
{
    _startColor = startColor;
    _endColor = endColor;
}


- (void)setText:(NSString *)text
{
    _text = text;
    _bgLabel.text = text;
    _contentLabel.text = text;
}

- (void)setFont:(UIFont *)font
{
    _font = font;
    _bgLabel.font = font;
    _contentLabel.font = font;
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    _bgLabel.textColor = textColor;
    _contentLabel.textColor = textColor;
}

- (UILabel *)bgLabel
{
    if (!_bgLabel) {
        _bgLabel = [[UILabel alloc] init];
        _bgLabel.textColor = Color_text_33;
        _bgLabel.text = @"昵称";
        _bgLabel.font = kFontRegular(14);
        _bgLabel.backgroundColor = [UIColor clearColor];
    }
    return _bgLabel;
}


- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = Color_text_33;
        _contentLabel.font = kFontRegular(14);
        _contentLabel.backgroundColor = [UIColor clearColor];
    }
    return _contentLabel;
}

- (UIImageView *)imageV
{
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        
    }
    return _imageV;
}


@end
